<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('角色管理')" />
</head>
<body>
<div class="wrapper">
    <form role="form" style="font-size: 12px" id="form" th:object="${role}">
        <div class="box-body">
            <input type="hidden" th:field="*{roleId}">
            <div class="row">
                <div class="col-xs-5">
                    <div class="form-group">
                        <label>角色名称</label>
                        <input type="text" class="form-control input-sm" th:field="*{roleName}">
                    </div>
                    <div class="form-group">
                        <label>权限字符</label>
                        <input type="text" class="form-control input-sm" th:field="*{roleKey}">
                    </div>
                    <div class="form-group">
                        <label>显示顺序</label>
                        <input type="number" class="form-control input-sm" th:field="*{roleSort}">
                    </div>
                </div>
                <div class="col-xs-7">
                    <label>菜单权限</label>
                    <div id="tree"></div>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = "/system/role";
    $(function() {
        var option = {
            id: 'tree',
            url: '/system/menu/tree/list',
            tid: 'menuId',
            tPid: 'parentId',
            tName: 'menuName'
        };
        $.tree.init(option);
    });
    function submitHandler() {
        if ($.validate.form()) {
            var json = $.common.formToJSON('form');
            json.menuIds = $.tree.getChecked();
            $.operate.save(prefix + '/save', json);
        }
    }
    $("#form").validate({
        rules:{
            onkeyup: false,
            roleName:{
                remote: {
                    url: "/system/role/checkRoleNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "roleId": function() {
                            return $("#roleId").val();
                        },
                        "roleName" : function() {
                            return $.common.trim($("#roleName").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            roleKey:{
                remote: {
                    url: "/system/role/checkRoleKeyUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "roleId": function() {
                            return $("#roleId").val();
                        },
                        "roleKey" : function() {
                            return $.common.trim($("#roleKey").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            roleSort:{
                digits:true
            }
        },
        messages: {
            "roleName": {
                remote: "角色名称已经存在"
            },
            "roleKey": {
                remote: "角色权限已经存在"
            }
        },
        focusCleanup: true
    });
    $('#tree').on("ready.jstree", function (e, data) {
        var role = [[${role}]];
        if(role && role.menuIds){
            $.tree.selectNode(role.menuIds);
        }
    });
</script>
</body>
</html>
